﻿CREATE PROCEDURE [dbo].[authors_validate]
    @authorId int = 0,
    @firstName nvarchar(256) = null,
    @lastName nvarchar(256) = null,
    @middleName nvarchar(256) = null
AS
BEGIN
    IF(@authorId = 0)
       RETURN CASE 
            WHEN EXISTS(
                SELECT Authors.AuthorId
                FROM Authors
                WHERE Authors.FirstName = @firstName AND Authors.LastName = @lastName
                    AND Authors.MiddleName = @middleName)
            THEN 0
            ELSE 1
       END
    ELSE 
        RETURN CASE 
            WHEN EXISTS(
                SELECT Authors.AuthorId
                FROM Authors
                WHERE Authors.AuthorId != @authorId AND Authors.FirstName = @firstName 
                    AND Authors.LastName = @lastName AND Authors.MiddleName = @middleName)
                THEN 0
                ELSE 1
            END
END
